#include "stdafx.h"
#include "Level.h"


Level::Level()
{
}


Level::~Level()
{
}

int Level::load(char* level_file)
{
	FILE* file = fopen(level_file, "r");
	int test;
	if (!file)
	{
		printf("File LevelInfo not found!");
		return -1;
	}

	// Read Skydome	
	for (int i = 0; i < 6; ++i)
		m_SkydomeInfo.TexturePath[i] = new char[100];
	fscanf(file, "Skydome: [NFG: %s ] [Tex: %s %s %s %s %s %s ]\n",
		m_SkydomeInfo.NFGPath,  m_SkydomeInfo.TexturePath[0], m_SkydomeInfo.TexturePath[1], m_SkydomeInfo.TexturePath[2],
								m_SkydomeInfo.TexturePath[3], m_SkydomeInfo.TexturePath[4], m_SkydomeInfo.TexturePath[5]);

	
	// Read terrain
	fscanf(file, "Terrain: [Heightmap: %s ] [Tex: count: %i",
			m_TerrainInfo.HeightMapPath, &m_TerrainInfo.TextureCount);
	m_TerrainInfo.TexturePath = new char*[m_TerrainInfo.TextureCount];
	for (int i = 0; i < m_TerrainInfo.TextureCount; ++i)
	{	
		m_TerrainInfo.TexturePath[i] = new char[100];
		fscanf(file, "%s", m_TerrainInfo.TexturePath[i]);
	}
	fscanf(file, " ]\n");
	
	
	// Read billboards
	fscanf(file, "NumBillboards: %i\n", &m_iNumBillboards);
	m_BillBoards = new ObjectInfo[m_iNumBillboards];
	for (int i = 0; i < m_iNumBillboards; ++i)
	{
		ObjectInfo* cur = &m_BillBoards[i];

		fscanf(file, "Detail: %s Position: [%f, %f, %f] Rotation: [%f, %f, %f] Scale: [%f, %f, %f]\n",
							cur->DetailInfo, &(cur->Position.x), &(cur->Position.y), &(cur->Position.z),
											 &(cur->Rotation.x), &(cur->Rotation.y), &(cur->Rotation.z),
											 &(cur->Scale.x), &(cur->Rotation.y), &(cur->Rotation.z));
	}

	// Read enemies
	fscanf(file, "NumEnemies: %i\n", &m_iNumEnemies);
	m_EnemyInfo = new ObjectInfo[m_iNumEnemies];
	for (int i = 0; i < m_iNumEnemies; ++i)
	{
		ObjectInfo* cur = &m_EnemyInfo[i];

		fscanf(file, "Detail: %s Position: [%f, %f, %f] Rotation: [%f, %f, %f] Scale: [%f, %f, %f]\n",
			cur->DetailInfo, &(cur->Position.x), &(cur->Position.y), &(cur->Position.z),
			&(cur->Rotation.x), &(cur->Rotation.y), &(cur->Rotation.z),
			&(cur->Scale.x), &(cur->Rotation.y), &(cur->Rotation.z));
	}

	// Read tower
	ObjectInfo* cur = &m_TowerInfo;
	fscanf(file, "Tower: \Detail: %s Position: [%f, %f, %f] Rotation: [%f, %f, %f] Scale: [%f, %f, %f]\n",
		cur->DetailInfo, &(cur->Position.x), &(cur->Position.y), &(cur->Position.z),
		&(cur->Rotation.x), &(cur->Rotation.y), &(cur->Rotation.z),
		&(cur->Scale.x), &(cur->Rotation.y), &(cur->Rotation.z));
	fclose(file);
	return 0;
}

int Level::save(char* level_file)
{
	FILE* file = fopen(level_file, "w");
	int test;
	if (!file)
	{
		printf("File LevelInfo not found!");
		return -1;
	}

	// Read Skydome	
	fprintf(file, "Skydome: [NFG: %s ] [Tex: %s %s %s %s %s %s ]\n",
		m_SkydomeInfo.NFGPath, m_SkydomeInfo.TexturePath[0], m_SkydomeInfo.TexturePath[1], m_SkydomeInfo.TexturePath[2],
		m_SkydomeInfo.TexturePath[3], m_SkydomeInfo.TexturePath[4], m_SkydomeInfo.TexturePath[5]);


	// Read terrain
	fprintf(file, "Terrain: [Heightmap: %s ] [Tex: count: %i",
		m_TerrainInfo.HeightMapPath, m_TerrainInfo.TextureCount);
	for (int i = 0; i < m_TerrainInfo.TextureCount; ++i)
	{
		fprintf(file, " %s", m_TerrainInfo.TexturePath[i]);
	}
	fprintf(file, " ]\n");


	// Read billboards
	fprintf(file, "NumBillboards: %i\n", m_iNumBillboards);
	for (int i = 0; i < m_iNumBillboards; ++i)
	{
		ObjectInfo* cur = &m_BillBoards[i];

		fprintf(file, "Detail: %s Position: [%f, %f, %f] Rotation: [%f, %f, %f] Scale: [%f, %f, %f]\n",
			cur->DetailInfo, (cur->Position.x), (cur->Position.y), (cur->Position.z),
			(cur->Rotation.x), (cur->Rotation.y), (cur->Rotation.z),
			(cur->Scale.x), (cur->Rotation.y), (cur->Rotation.z));
	}

	// Read enemies
	fprintf(file, "NumEnemies: %i\n", m_iNumEnemies);
	for (int i = 0; i < m_iNumEnemies; ++i)
	{
		ObjectInfo* cur = &m_EnemyInfo[i];

		fprintf(file, "Detail: %s Position: [%f, %f, %f] Rotation: [%f, %f, %f] Scale: [%f, %f, %f]\n",
			cur->DetailInfo, (cur->Position.x), (cur->Position.y), (cur->Position.z),
			(cur->Rotation.x), (cur->Rotation.y), (cur->Rotation.z),
			(cur->Scale.x), (cur->Rotation.y), (cur->Rotation.z));
	}

	// Read tower
	ObjectInfo* cur = &m_TowerInfo;
	fprintf(file, "Tower: \Detail: %s Position: [%f, %f, %f] Rotation: [%f, %f, %f] Scale: [%f, %f, %f]\n",
		cur->DetailInfo, (cur->Position.x), (cur->Position.y), (cur->Position.z),
		(cur->Rotation.x), (cur->Rotation.y), (cur->Rotation.z),
		(cur->Scale.x), (cur->Rotation.y), (cur->Rotation.z));
	fflush(file);
	fclose(file);
	return 0;
}